<template>
  <div class="item-list">
    <h1>物品列表</h1>
    <div>
      <el-table :data="tableData">
        <el-table-column prop="_id" label="ID" width="150"  >
        </el-table-column>
        <el-table-column prop="icon" label="icon" width="120">
          <template slot-scope="scope">
            <img :src="scope.row.icon" alt="物品icon" style="width:4rem" >
          </template>
        </el-table-column>
        <el-table-column prop="name" label="物品名称" width="120">
        </el-table-column>
        <el-table-column label="分类名称" width="200">
          <template v-if="scope.row.category"  slot-scope="scope">
            <i class="el-icon-folder"></i>
            装备介绍->{{scope.row.category.name}}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="160">
          <template slot-scope="scope">
            <el-button type="primary" @click="$router.push(`/items/edit/${scope.row._id}`)" size="small">编辑</el-button>
            <el-button type="danger" @click="remove(scope.row)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
export default {
  name: 'ItemList',
  data () {
    return {
      tableData: []
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    async fetch () {
      const result = await this.$http.get('/rest/items')
      if(result.data){
         this.tableData = result.data
      }
    },
    async remove (row) {
      this.$confirm(`是否要删除分类"${row.name}"?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const result = await this.$http.delete(`/rest/items/${row._id}`)
        if(result.data.success){
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetch()
        }else{
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        }
      }).catch(()=>{
        
      })
    }
  }
}
</script>
<style scoped>
.item-list>>>.el-table .el-table_1_column_1  .cell{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
